package com.bestescortbedsystem.service;

import com.bestescortbedsystem.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * ClassName: Sf
 * Package: com.bestescortbedsystem
 *
 * @author: guanzepeng
 * @Date: 2021/3/8 16:55 星期一
 * @Description:
 */
@Service
public class Sf {

	public void setKey(String phone) {
		SecureRandom random2 = new SecureRandom();
		String random = String.valueOf(random2.nextInt());
		Jedis jedis = new Jedis("192.168.188.9");
		jedis.set(phone, random);
		jedis.expire(phone, 3600);
		String host = "https://smssend.shumaidata.com";
		String path = "/sms/send";
		String method = "POST";
		String appcode = "43bc0ccace0a401c99c5e3654ffeb379";
		Map<String, String> headers = new HashMap<String, String>();
		//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("receive", phone);
		querys.put("tag", random);
		querys.put("templateId", "M09DD535F4");
		Map<String, String> bodys = new HashMap<String, String>();


		try {
			/**
			 * 重要提示如下:
			 * HttpUtils请从
			 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
			 * 下载
			 *
			 * 相应的依赖请参照
			 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
			 */
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			System.out.println(response.toString());
			//获取response的body
			System.out.println(EntityUtils.toString(response.getEntity()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Boolean getKey(String phone, String random) {
		Jedis jedis = new Jedis("192.168.188.9");
		String s = jedis.get(phone);
		if(random.equals(s)) {
			return true;
		}
		return false;
	}
	}


